/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import com from '@system.CommunicationKit';
import router from '@system.router'

export default {
    data: {
        isPowerOn:false,
        isWifi:true,
    },
    onInit() {
            setTimeout(()=>{
                setInterval(()=>this.GetKey(),500)
            },6000);
    },

    clickPower(){
        this.isPowerOn = !this.isPowerOn
        if (this.isPowerOn) {
            this.SetKey( 'key_f1', 'on' );
        } else {
            this.SetKey( 'key_f1', 'off' );
        }
    },



    SetKey(key1, value1) {
        com.set({
            key: key1 + '',
            value: value1 + '',
            success: function() {
                console.log('call storage.set success.');
            },
            fail: function(data, code) {
                console.log('call storage.set fail, code: ' + code + ', data: ' + data);
            },
        });
    },

    GetKey() {
        com.get({
            key: 'storage_key',
            success: (data)=> {
                let res = JSON.parse(data)

                if (res.key) {
                    if(res.key == 'on'){
                        this.isPowerOn = true;
                    }else{
                        this.isPowerOn = false;
                    }
                }

                if (res.wifi) {
                    if(res.wifi == 'connected') {
                        this.isWifi = true;
                    } else {
                        this.isWifi = false;
                    }
                }

            },
            fail: function(data, code) {
                console.log('call storage.get fail, code: ' + code + ', data: ' + data);
            },
            complete: function() {
                console.log('call complete');
            },
        });
    },


    changePage(operation) {
        router.replace({
            uri:"pages/dm/dm"
        });
    }
}
